{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hide_input": true
   },
   "outputs": [],
   "source": [
    "import ipywidgets as widgets\n",
    "from ipywidgets import interact, interactive, fixed, interact_manual\n",
    "\n",
    "import numpy as np\n",
    "from plotly.offline import init_notebook_mode, iplot, iplot_mpl\n",
    "import plotly\n",
    "import plotly.graph_objects as go"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "init_notebook_mode()\n",
    "\n",
    "import ipywidgets as widgets\n",
    "from IPython.display import display, HTML\n",
    "\n",
    "javascript_functions = {False: \"hide()\", True: \"show()\"}\n",
    "button_descriptions  = {False: \"Show code\", True: \"Hide code\"}\n",
    "\n",
    "\n",
    "def toggle_code(state):\n",
    "\n",
    "    \"\"\"\n",
    "    Toggles the JavaScript show()/hide() function on the div.input element.\n",
    "    \"\"\"\n",
    "\n",
    "    output_string = \"<script>$(\\\"div.input\\\").{}</script>\"\n",
    "    output_args   = (javascript_functions[state],)\n",
    "    output        = output_string.format(*output_args)\n",
    "\n",
    "    display(HTML(output))\n",
    "\n",
    "\n",
    "def button_action(value):\n",
    "\n",
    "    \"\"\"\n",
    "    Calls the toggle_code function and updates the button description.\n",
    "    \"\"\"\n",
    "\n",
    "    state = value.new\n",
    "\n",
    "    toggle_code(state)\n",
    "\n",
    "    value.owner.description = button_descriptions[state]\n",
    "\n",
    "\n",
    "state = False\n",
    "toggle_code(state)\n",
    "\n",
    "button = widgets.ToggleButton(state, description = button_descriptions[state])\n",
    "button.observe(button_action, \"value\")\n",
    "\n",
    "display(button)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Demo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "调整参数t和L，点击Run Interact来生成图像"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def figure(t: float, L:int):\n",
    "    X, Y, Z = np.mgrid[-1:1:41j, -1:1:41j, -1:1:41j]\n",
    "    values = 10*(np.cos(2*np.pi/L*Y)*np.cos(2*np.pi/L*Z)+np.cos(2*np.pi/L*X)*np.cos(2*np.pi/L*Z)+np.cos(2*np.pi/L*X)*np.cos(2*np.pi/L*Y))-5*(np.cos(4*np.pi/L*X)+np.cos(4*np.pi/L*Y)+np.cos(4*np.pi/L*Z))\n",
    "    fig = go.Figure(data=go.Isosurface(\n",
    "        x=X.flatten(),\n",
    "        y=Y.flatten(),\n",
    "        z=Z.flatten(),\n",
    "        value=values.flatten(),\n",
    "        isomin=t,\n",
    "    #    isomax=40,\n",
    "        caps=dict(x_show=True, y_show=True)\n",
    "        ))\n",
    "    return fig\n",
    "\n",
    "\n",
    "\n",
    "def draw(t: float, L: int):\n",
    "    iplot(figure(t, L), filename='jupyter-basic_bar')\n",
    "\n",
    "L_slider = widgets.IntSlider(min=0, max=4, step=1, value=2, description=\"param L\")\n",
    "t_slider = widgets.FloatSlider(min=8., max=12., step=0.1, value=10.0, description=\"param t\")\n",
    "interact_manual(draw, t=t_slider, L=L_slider)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}